package com.xdxc.component.validation;

import com.xdxc.annotation.AnswerValidationRule;
import com.xdxc.model.dto.UserAnswerDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static com.xdxc.utils.convertToLongList.convertToLongList;

// 选择题校验（必须包含选项ID）
@AnswerValidationRule(quType = 1)
@Component
public class ChoiceQuestionValidator implements AnswerValidator {
    @Override
    public boolean validate(UserAnswerDto dto) {
        // 1. 基础校验：DTO非空，关键字段非空
        if (dto == null || dto.getCheckId() == null || dto.getQuId() == null) {
            return false;
        }
        List<?> rawAnswerIds = dto.getAnswerIds();


        // 2. 校验answerIds
        // 2.1 必须是非空集合
        if (CollectionUtils.isEmpty(rawAnswerIds)||rawAnswerIds.size()>=2) {
            return false;
        }

        try {
            List<Long> answerIds = convertToLongList(rawAnswerIds);
            return answerIds != null; // 转换成功才返回 true
        } catch (Exception e) {
            return false; // 转换失败返回 false
        }
    }}

